package a_aa_amainpackage.qiye_test.address

import org.apache.spark.sql.{SQLContext, SparkSession}

object obsUtils {
  /**
    * 获取schema信息
     * @param path
    * @return
    */
  def getOrcSchema(path: String): String = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("Transtor")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .getOrCreate()
    var sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    sc.setLogLevel("ERROR")
    val sqlContext: SQLContext = spark.sqlContext
    val schema = sqlContext.read.orc(path ).withColumnRenamed("tid","tid1")
      .schema
    val string = schema.treeString
    val str = string.replaceAll("\\(nullable = true\\)", ",")
      .replaceAll("\\|--", "")
      .replaceAll(":", "")
      .replaceAll("string", "text")
      .replaceAll("struct", "text")
      .replaceAll("double","float8")
      .replaceAll("long ,","bigint ,")
      .replace("root\n","")
    val str1 = str.substring(0,str.length-2)
    spark.stop()
    str1

  }

  /**
    * 建表
     * @param orc_schem
    * @param schem
    * @param plat
    * @param month
    * @param path
    * @return
    */
  def sql(orc_schem:String,schem:String,plat:String,month:String,path:String):String = {


    /**
      *
      * "test_liu"."g_2019_kaola_07"
      *
      *
      * "test_liu"."g_2019_feizhu_mp_shuang11"
      *
      * "${schem}"."g_2019_${plat}_shuang${month}"
      * "zf_test"."g_2019_tmall_shop_10"
      * "test_liu"."g_2018_elm_12"
      */
    //店铺
//     var s1 =
//       s"""
//         |DROP FOREIGN TABLE IF EXISTS "${schem}"."g_2021_${plat}_shop_${month}";
//         |CREATE foreign TABLE "${schem}"."g_2021_${plat}_shop_${month}"
//         |(
//         | ${orc_schem}
//         |) SERVER obs_server
//         |OPTIONS (
//         |format 'orc',
//         |encoding 'utf8',
//         |foldername '${path}',
//         |totalrows '213328'
//         |)
//         |DISTRIBUTE BY ROUNDROBIN;
//         """.stripMargin
    //小平台单品
    var s1 =
      s"""
         |DROP foreign TABLE IF EXISTS "${schem}"."g_2021_${plat}_${month}_bu";
         |CREATE foreign TABLE "${schem}"."g_2021_${plat}_${month}_bu"
         |(
         | ${orc_schem}
         |) SERVER obs_server
         |OPTIONS (
         |format 'orc',
         |encoding 'utf8',
         |foldername '${path}',
         |totalrows '213328'
         |)
         |DISTRIBUTE BY ROUNDROBIN;
         """.stripMargin
    println(s1)
s1

  }





}
